<?php
include("../autoload.php");

$codUsuario = $_POST['cmbUsuario'];
$dataInicial = Util::dataToMysql($_POST['txtDataInicial']);
$dataFinal = Util::dataToMysql($_POST['txtDataFinal']);
$strPeriodo = "";
$strWhere = array();

if($codUsuario){
    $strWhere[] = " codusuario = '$codUsuario'";
}

if($dataInicial){
    $strWhere[] = " SUBSTRING(datahora,1,10) >= '$dataInicial'";
    $strPeriodo = "A partir de ".Util::dataToBR($dataInicial);
}

if($dataFinal){
    $strWhere[] = " SUBSTRING(datahora,1,10) <= '$dataFinal'";
    $strPeriodo = "Anterior a ".Util::dataToBR($dataFinal);
}

if(($dataInicial) && ($dataFinal)){
    $strPeriodo = Util::dataToBR($dataInicial)." at&eacute; ".Util::dataToBR($dataFinal);
}

if(count($strWhere) > 0){
    $strWhere = implode(" AND",$strWhere);
    $strWhere = "WHERE ".$strWhere;
}

$listaLogs = Logs::Listalogs(" $strWhere ");
if($listaLogs){
?>
<title>Relat&oacute;rio de Logs</title>
<table width="750" style="border: 1px solid #000; margin-bottom: 15px;">
    <?php
    if($strPeriodo != ""){
    ?>
    <tr align="left">
        <td colspan="2">Per&iacute;odo: <?php echo $strPeriodo;?></td>
    </tr>
    <?php
    }
    ?>
</table>

<table width="750" style="border: 1px solid #000;">
    <tr align="center">
        <td width="140"><strong>Data/hora</strong></td>
        <td width="250"><strong>Usu&aacute;rio</strong></td>
        <td width="70"><strong>IP</strong></td>
        <td><strong>A&ccedil;&atilde;o</strong></td>
    </tr>
    <tr>
        <td colspan="4"><hr size="1" /></td>
    </tr>
    <?php
        foreach ($listaLogs as $logs){
            $aux = explode(" ",$logs->getDatahora());
            $data = $aux[0];
            $hora = $aux[1];
            
            $dadosUsuario = new Usuarios($logs->getCodusuario());
        ?>
            <tr>
                <td><?php echo Util::dataToBR($data)." ".$hora;?></td>
                <td><?php echo $dadosUsuario->getNome();?></td>
                <td><?php echo $logs->getIp();?></td>
                <td><?php echo $logs->getAcao();?></td>
            </tr>
    <?php
    }
?>
</table>
<?php
}else{
    echo "<center>N&atilde;o h&aacute; resultados</center>";
}
?>